
layui.use(['form','jquery','table','layer','upload'],function() {
    var form = layui.form
        , $ = layui.jquery
        , table = layui.table
        , layer = layui.layer
        , upload = layui.upload;


    //获取当前用户信息
    var updateBy = JSON.parse(localStorage.getItem("user")).trueName;

    /*课程分类弹窗----------------*/
    courseClassification = function courseClassification() {
        layer.open({
            type: 2
            ,title: '选择课程分类'
            ,content: 'courseClassification.html'
            ,area: ['500px','350px']
        })
    }

    /*获取当前时间*/
    var updateTime;
    getTime();
    function getTime(){
        var myDate = new Date();
        var myYear = myDate.getFullYear(); //获取完整的年份(4位,1970-????)
        var myMonth = myDate.getMonth()+1; //获取当前月份(0-11,0代表1月)
        var myToday = myDate.getDate(); //获取当前日(1-31)
        var myDay = myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
        var myHour = myDate.getHours(); //获取当前小时数(0-23)
        var myMinute = myDate.getMinutes(); //获取当前分钟数(0-59)
        var mySecond = myDate.getSeconds(); //获取当前秒数(0-59)
        updateTime = myYear+'-'+myMonth+'-'+myToday+' '+myHour+':'+myMinute+':'+mySecond;
    };


    //课程id
    var courseId = parent.$('#parentId').val();

    //课程id存入隐藏域
    $('input[name=courseId]').val(courseId);

    //修改钱现价
    var beforCourseNprice;


    $.ajax({
        url: '/course/getCourseById'
        ,data: {'courseId': courseId}
        ,dataType: 'json'
        ,success:function (data) {
            var course = data.data;

            $('#classId').val(course.classId);
            $('#name').val(course.name);

            $('#dictSort').val(course.id);

            $('#courseName').val(course.courseName);
            $('#createDate').val(course.createDate);
            $('#createId').val(course.createId);
            $('#courseNprice').val(course.courseNprice);
            beforCourseNprice = $('#courseNprice').val();
            $('#coursePrimary').val(course.coursePrimary);
            //课程关键字变量 赋值
            coursePrimary = $('#coursePrimary').val();
            $('#courseIntro').val(course.courseIntro);
            $('#courseInfo').val(course.courseInfo);
            $('img[name=img]').attr('src',course.url);

        }
    });



    /*修改提交*/
    form.on('submit(sub)',function (data) {
        var course = data.field;
        var dictSort = $('#dictSort').val();
        //判断是否修改价格
        if(course.courseNprice > beforCourseNprice || course.listClass < beforCourseNprice){
            var trueName = JSON.parse(localStorage.getItem('user')).trueName;
            var addJournal = {
                 'dictType': '13'
                ,'trueName': trueName
                ,'updateTime': updateTime
                ,'beforCourseNprice': beforCourseNprice
                ,'dictSort': dictSort
                ,'courseNprice': course.courseNprice
            }
            $.ajax({
                 url: '/course/addCourseJournal'
                ,data: addJournal
                ,dataType: 'json'
                ,success:function (data) {

                }
            });
        }
        $.ajax({
            url: '/course/courseEdit'
            ,data: data.field
            ,dataType: 'json'
            ,success:function (data) {
                if(data.data){
                    //添加操作日志
                    $.ajax({
                        url: '/course/addoperationLog'
                        ,data: {
                            'dictSort': 3
                            ,'updateBy': updateBy
                            ,'updateTime': updateTime
                            ,'remark': '执行了编辑操作'
                        }
                        ,dataType: 'json'
                        ,success:function(data){

                        }
                    });
                    var layer = parent.layer;
                    layer.close(layer.index);
                    layer.msg('修改成功');
                }
            }
        });
    });








    //取消按钮
    $('#cancel').click(function () {
        var layer = parent.layer;
        layer.close(layer.index);
    });


    //上传预览图
    var uploadInst = upload.render({
         elem: '#uploadEdit'
        ,auto: true
        ,url: '/Upload/upload'
        ,field: 'file'
        ,choose:function(obj){
            var files = obj.pushFile();
            obj.preview(function(index,file,result){
                $('img[name=img]').attr('src',result);
            })
        }
        ,done:function (res) {
            var src = res.data;
            $('input[name=url]').val(src);
        }
    });


});